package im.starchat.chat.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.adapter.ChannelAdapter;
import im.starchat.chat.impl.IFollowingCallback;
import im.starchat.chat.rest.RestClient;
import im.starchat.chat.rest.model.ChannelBean;
import im.starchat.chat.rest.model.api.NullResponse;
import im.starchat.chat.view.swipeRefreshLayout.SwipeRefreshLayout;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by liuchao on 2/3/16.
 * 预告 fragment
 */
public class ChannelFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{

    private static final String TAG = "ParadeFragment";


    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.swipe_refresh_layout_list)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private ChannelAdapter mChannelAdapter;
    private LinearLayoutManager mLayoutManager;
    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;
    // recommend 推荐用户 hot 人气用户 newest 最新入驻
    private static final String TYPE = "type";

    public ChannelFragment() {
    }

    public static ChannelFragment newInstance(String type) {
        ChannelFragment fragment = new ChannelFragment();
        Bundle bundle = new Bundle();
        bundle.putString( TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_plaza_single, container, false);
        ButterKnife.inject(this, view);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);


        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mChannelAdapter = new ChannelAdapter(getActivity(), new IFollowingCallback() {
            @Override
            public void onFollowing(String id, int position) {
                following(id, position);
            }

            @Override
            public void onCancelFollowing(String id, int position) {
                cancelFollowing(id, position);
            }
        });
        mRecyclerView.setAdapter(mChannelAdapter);

        getPreviewList();
        mSwipeRefreshLayout.setRefreshing(true);

        return view;
    }

    private void getPreviewList(){
        XLApplication.getInstance().getRestClient().getStars(getArguments().getString(TYPE), mCurrentPage, RestClient.DEFAULT_PREPAGE, new Callback<ChannelBean[]>() {
            @Override
            public void success(ChannelBean[] channelBean, Response response) {
                if (mCurrentPage == RestClient.DEFAULT_PAGE) {
                    mChannelAdapter.refreshData(new ArrayList<ChannelBean>(Arrays.asList(channelBean)));
                } else {
                    mChannelAdapter.addAll(Arrays.asList(channelBean));
                }
                if (channelBean.length < 20) {
                    mHasMoreData = false;
                }else{
                    mHasMoreData = true;
                }
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }

            @Override
            public void failure(RetrofitError error) {
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }
        });
    }

    private void following(String id, final int position){
        XLApplication.getInstance().getRestClient().following(id,"",  new Callback<NullResponse>() {
            @Override
            public void success(NullResponse nullResponse, Response response) {
                Toast.makeText(getActivity(), "关注成功", Toast.LENGTH_SHORT).show();
                mChannelAdapter.changeAttentionState(position, true);
                View view=mRecyclerView.getLayoutManager().findViewByPosition(position);
                ChannelAdapter.ParadeHolder holder= (ChannelAdapter.ParadeHolder) mRecyclerView.getChildViewHolder(view);
                if(holder!=null){
                    holder.mCancelAttentionBtn.setVisibility(View.VISIBLE);
                    holder.mAttentionBtn.setVisibility(View.GONE);
                }
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(getActivity(), "关注失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void cancelFollowing(String id, final int position){
        XLApplication.getInstance().getRestClient().delFollowing(id, new Callback<NullResponse>() {
            @Override
            public void success(NullResponse nullResponse, Response response) {
                Toast.makeText(getActivity(), "取消关注成功", Toast.LENGTH_SHORT).show();
                mChannelAdapter.changeAttentionState(position, false);
                View view=mRecyclerView.getLayoutManager().findViewByPosition(position);
                ChannelAdapter.ParadeHolder holder= (ChannelAdapter.ParadeHolder) mRecyclerView.getChildViewHolder(view);
                if(holder!=null){
                    holder.mCancelAttentionBtn.setVisibility(View.GONE);
                    holder.mAttentionBtn.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(getActivity(), "取消关注失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onRefresh() {
        mCurrentPage = RestClient.DEFAULT_PAGE;
        getPreviewList();
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
            getPreviewList();
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            Toast.makeText(getActivity(), "没有更多数据", Toast.LENGTH_SHORT).show();
        }

    }

}
